package com.googlecode.animeclassifier.attribute;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 *  Parse bracket string and return respective attribute which could be more than one.
 * 
 *  Using fly weight and simple factory pattern.
 *
 */
public class AttributeFactory {
	
	/**
	 * Used like hash map, but the key is index.
	 */
	private static Map<String, Attribute> attrPool = new HashMap<String, Attribute>();
	
	private static List<Rule> rules;
	
	static {
		rules = new ArrayList<Rule>();
		rules.addAll(RuleReader.getRule());
	}
	
	public static List<Attribute> parse(String bracketString) {
		List<Attribute> returnList = new LinkedList<Attribute>();
		for(Rule r: rules) {
			if(r.pattern.matcher(bracketString).matches()) {
				//if has
				if(attrPool.get(r.type + bracketString) != null) returnList.add(attrPool.get(r.type + bracketString));
				else { //hasn't
					Attribute attr;
					if(r.value != null)
						attr = new AttributeImpl(r.value, r.type);
					else
						attr = new AttributeImpl(bracketString, r.type);
					
					attrPool.put(r.type + bracketString, attr);
					returnList.add(attr);
				}
			}
		}
		return returnList;
	}
}

class VolumeAttribute implements Attribute {
	int vol;

	public VolumeAttribute(String vol) {
		this.vol = Integer.parseInt(vol);
	}
	
	@Override
	public String getString() {
		return Integer.toString(vol);
	}

	@Override
	public Attributes getType() {
		return Attributes.VOLUME;
	}
	
	public String toString() {
		return Integer.toString(vol);
	}
	
}